﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ERP.Orders;
using Mengsk.ERP.Database;
using Mengsk.ERP.Orders.TDOrderInterfaces;

namespace Mengsk.ERP.App.Orders.OrderView
{
    /// <summary>
    /// OrderViewUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class OrderViewUserControl : UserControl, IUIPlugin
    {
        private OrderViewModel vm = new OrderViewModel();
        private OrderViewModel scanVM = new OrderViewModel();
        private List<Order> scanOrders = new List<Order>();

        public OrderViewUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "订单分析"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            DateTime? date = this.dpDate.SelectedDate;
            if (date == null)
            {
                MessageBox.Show("选择时间");
                return;
            }

            try
            {
                Order[] orders = OrderManager.Instance.GetOrdersByDate(date.Value);
                foreach (Order order in orders)
                {
                    Order onlineOrder = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).DownLoadOrder(order.POPID, order.CreateTime, false);
                    order.BuyerComment = onlineOrder.BuyerComment;
                    order.SellerComment = onlineOrder.SellerComment;
                    order.Flag = onlineOrder.Flag;
                    order.State = onlineOrder.State;
                    order.Address = onlineOrder.Address;
                }
                this.vm = OrderViewModel.Anlay(orders);
                if (this.vm == null)
                {
                    MessageBox.Show("错误分析没有返回数据");
                    this.vm = new OrderViewModel();
                    return;
                }

                this.tbRefused.Text = ((this.vm.OrdersRefusedNotReturned.Length + this.vm.OrdersRefusedReturned.Length) * 100.0 / (this.vm.Orders.Length - this.vm.OrdersNonDelivery.Length)).ToString("F2") + "%";
                this.tbAccept.Text = ((this.vm.OrdersSign.Length + this.vm.OrdersSucess.Length) * 100.0 / (this.vm.Orders.Length - this.vm.OrdersNonDelivery.Length)).ToString("F2") + "%";
                this.tbTotal.Text = this.vm.Orders.Length.ToString();
                this.btnWaitShip.Content = "等待发货 " + this.vm.OrdersWaitShip.Length;
                this.btnShipOK.Content = "已经发货 " + this.vm.OrdersShipOK.Length;
                this.btnSign.Content = "已经签收 " + this.vm.OrdersSign.Length;
                this.btnSucess.Content = "已经返款 " + this.vm.OrdersSucess.Length;
                this.btnRefusedReturned.Content = "拒收已返回 " + this.vm.OrdersRefusedReturned.Length;
                this.btnRefusedNotReturned.Content = "拒收未返回 " + this.vm.OrdersRefusedNotReturned.Length;
                this.btnEmptyDelivery.Content = "没有物流订单号 " + this.vm.OrdersNonDelivery.Length;
                this.btnDeliveryDuplicate.Content = "物流订单重复 " + this.vm.OrdersDuplicateDelivery.Length;
                this.orderDetailUserControl1.Orders = null;
                this.tbCurrentView.Text = "";
                MessageBox.Show("分析完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ShowOrders(Order[] orders, string title)
        {
            if (orders == null)
            {
                MessageBox.Show("请先查询数据");
                return;
            }

            try
            {
                this.tbCurrentView.Text = "当前显示订单: " + title;
                this.orderDetailUserControl1.Orders = orders;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnWaitShip_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersWaitShip, (sender as Button).Content.ToString());
        }

        private void btnShipOK_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersShipOK, (sender as Button).Content.ToString());
        }

        private void btnSign_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersSign, (sender as Button).Content.ToString());
        }

        private void btnSucess_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersSucess, (sender as Button).Content.ToString());
        }

        private void btnRefusedReturned_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersRefusedReturned, (sender as Button).Content.ToString());
        }

        private void btnRefusedNotReturned_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersRefusedNotReturned, (sender as Button).Content.ToString());
        }

        private void btnEmptyDelivery_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersNonDelivery, (sender as Button).Content.ToString());
        }

        private void btnDeliveryDuplicate_Click(object sender, RoutedEventArgs e)
        {
            this.ShowOrders(this.vm.OrdersDuplicateDelivery, (sender as Button).Content.ToString());
        }

        private void tbDeliveryNumber_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }

            e.Handled = true;

            string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
            if (string.IsNullOrWhiteSpace(deliveryNumber))
            {
                return;
            }

            try
            {
                Order[] orders = OrderManager.Instance.GetOrdersByDeliveryNumber(deliveryNumber);
                foreach (Order order in orders)
                {
                    Order onlineOrder = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).DownLoadOrder(order.POPID, order.CreateTime, false);
                    order.BuyerComment = onlineOrder.BuyerComment;
                    order.SellerComment = onlineOrder.SellerComment;
                    order.Flag = onlineOrder.Flag;
                    order.State = onlineOrder.State;
                    order.Address = onlineOrder.Address;
                }
                this.scanOrderDetailUserControl.Orders = orders;
                this.tbDeliveryNumber.Text = "";

                if (orders.Length < 1)
                {
                    MessageBox.Show("未找到订单，使用本界面的QQ搜索后，手动更新物流单号");
                }

                if (orders.Length > 1)
                {
                    MessageBox.Show("该物流单号找到多个订单");
                }

                if (orders.Length == 1)
                {
                    if (this.scanOrders.Contains(orders[0]))
                    {
                        this.scanOrders.Remove(orders[0]);
                    }
                    this.scanOrders.Add(orders[0]);
                    this.UpdateView();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbName_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }

            e.Handled = true;

            string name = this.tbName.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }

            try
            {
                Order[] orders = OrderManager.Instance.GetOrdersByName(name);
                foreach (Order order in orders)
                {
                    Order onlineOrder = TDOrderInterfaceBase.GetOrderInterfaceSource(order.Shop).DownLoadOrder(order.POPID, order.CreateTime, false);
                    order.BuyerComment = onlineOrder.BuyerComment;
                    order.SellerComment = onlineOrder.SellerComment;
                    order.Flag = onlineOrder.Flag;
                    order.State = onlineOrder.State;
                    order.Address = onlineOrder.Address;
                }
                this.scanOrderDetailUserControl.Orders = orders;
                this.tbDeliveryNumber.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            this.scanOrders.Clear();
            this.UpdateView();
            this.scanOrderDetailUserControl.Orders = null;
        }

        private void UpdateView()
        {
            OrderViewModel vm = OrderViewModel.Anlay(this.scanOrders.ToArray());

            this.tbWaitShip.Text = vm.OrdersWaitShip.Length.ToString();
            this.tbShipOK.Text = vm.OrdersShipOK.Length.ToString();
            this.tbSign.Text = vm.OrdersSign.Length.ToString();
            this.tbSucess.Text = vm.OrdersSucess.Length.ToString();
            this.tbRefuseNotReturn.Text = vm.OrdersRefusedNotReturned.Length.ToString();
            this.tbRefuseReturned.Text = vm.OrdersRefusedReturned.Length.ToString();
            this.tbScanTotal.Text = "总数:" + this.scanOrders.Count.ToString();
            if (this.scanOrders.Count > 0)
            {
                this.tbRefusedCal.Text = ((vm.OrdersRefusedNotReturned.Length + vm.OrdersRefusedReturned.Length) * 100.0 / (vm.Orders.Length - vm.OrdersNonDelivery.Length)).ToString("F2") + "%";
                this.tbAccpetCal.Text = ((vm.OrdersSign.Length + vm.OrdersSucess.Length) * 100.0 / (vm.Orders.Length - vm.OrdersNonDelivery.Length)).ToString("F2") + "%";
            }
            else
            {
                this.tbRefusedCal.Text = "0";
                this.tbAccpetCal.Text = "0";
            }
        }

      
    }
}
